package com.liyongjian.apidoc.mvc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liyongjian.apidoc.dal.ApiRepository;
import com.liyongjian.apidoc.dal.ModelRepository;
import com.liyongjian.apidoc.dal.ParamRepository;
import com.liyongjian.apidoc.dal.ResRepository;
import com.liyongjian.apidoc.dal.SysRepository;
import com.liyongjian.apidoc.domain.Api;
import com.liyongjian.apidoc.domain.Model;
import com.liyongjian.apidoc.domain.Param;
import com.liyongjian.apidoc.domain.Res;
import com.liyongjian.apidoc.domain.Sys;
import com.liyongjian.apidoc.domain.enumm.ContentType;
import com.liyongjian.apidoc.domain.enumm.LiveDieState;
import com.liyongjian.apidoc.exception.ServerException;
import com.liyongjian.apidoc.utils.MapUtils;


@Controller
@RequestMapping("doc")
public class DocController extends BaseController {

    @Autowired
    private SysRepository sysRepository;
    @Autowired
    private ApiRepository apiRepository;
    @Autowired
    private ParamRepository paramRepository;
    @Autowired
    private ResRepository resRepository;
    @Autowired
    private ModelRepository modelRepository;

    @RequestMapping("sys")
    public String sys(Long sysId, HttpServletRequest request, HttpServletResponse response,
            ModelMap model) {
        Sys sys = sysRepository.findOne(sysId);
        if (sys == null || sys.state == LiveDieState.DIE) {
            throw new ServerException("");
        }
        model.put("sys", sys);
        List<Map<String, Object>> modelList = new ArrayList<>();
        List<Model> models = modelRepository.findBySysIdAndState(sysId, LiveDieState.LIVE);
        Model ms = new Model();
        ms.name = "默认模块";
        models.add(0, ms);
        for (Model m : models) {
            List<Api> apis = apiRepository.findByUserIdAndSysIdAndModelIdAndState(sys.userId,
                    sys.id, m.id, LiveDieState.LIVE);
            List<Map<String, Object>> apiList = new ArrayList<>();
            for (Api api : apis) {
                List<Param> params = paramRepository.findByApiIdAndState(api.id, LiveDieState.LIVE);
                List<Param> getParams = new ArrayList<>();
                List<Param> bodyParams = new ArrayList<>();
                for (Param param : params) {
                    if (param.contentType == ContentType.GET) {
                        getParams.add(param);
                    } else {
                        bodyParams.add(param);
                    }

                }
                Map<String, Object> apiMap = MapUtils.convert(api);
                apiMap.put("getParams", getParams);
                apiMap.put("bodyParams", bodyParams);

                List<Res> ress = resRepository.findByApiIdAndState(api.id, LiveDieState.LIVE);
                apiMap.put("ress", ress);
                apiList.add(apiMap);
            }
            Map<String, Object> modelMap = MapUtils.convert(m);
            modelMap.put("apis", apiList);
            modelList.add(modelMap);
        }
        model.put("modelList", modelList);
        return "doc/sys";
    }



}
